home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_309 / csh / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  203 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  * Matthew Dillon, 24 Feb 1986
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  *
  9.  * Version 4.00A by Carlo Borreo & Cesare Dieni 13-Jan-90
  10.  *
  11.  */
  12.  
  13. char shellname[]="Shell V4.00A";
  14. char shellctr []="CshCounter";
  15.  
  16. int aux; /* for use with aux: driver */
  17. char *oldtitle;
  18. char trueprompt[100];
  19. char Inline[260];
  20. struct IntuitionBase *IntuitionBase;
  21. struct Window *w;
  22. struct ArpBase *ArpBase;
  23.  
  24. main(argc, argv)
  25. register char *argv[];
  26. {
  27. #if RAW_CONSOLE
  28.     char *rawgets();
  29. #endif
  30.  
  31. register unsigned int i;
  32. extern int Enable_Abort;
  33. char buf[10];
  34. static char pipe1[32], pipe2[32];
  35. struct Window *getwindow();
  36.  
  37. ArpBase=(struct ArpBase *)OpenLibrary("arp.library",34L);
  38. if (ArpBase==NULL) { printf("No arp library\n"); exit(0); }
  39.  
  40. Forbid();
  41. i=0;
  42. if (Getenv(shellctr,buf,10L)) {
  43.     i=(int)(long)Atol(buf);
  44.     if (IoErr()) i=0;
  45.     }
  46. sprintf(buf,"%d",i+1);
  47. Setenv(shellctr,buf);
  48. Permit();
  49.  
  50. IntuitionBase=(struct IntuitionBase *)ArpBase->IntuiBase;
  51.  
  52. stdin->_flags    |= 0x80;    /* make sure we're set as a tty */
  53. stdout->_flags    |= 0x80;    /* in case of redirection in .login */
  54. Close(_devtab[2].fd);
  55. _devtab[2].mode |= O_STDIO;
  56. _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  57.                 /* don't work with aux driver */
  58. Myprocess = (struct Process *)FindTask(0L);
  59. Mycli=(struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);
  60. w=getwindow();
  61. Pipe1 = pipe1;
  62. Pipe2 = pipe2;
  63. sprintf(pipe1, "ram:pipe1_%ld", Myprocess);
  64. sprintf(pipe2, "ram:pipe2_%ld", Myprocess);
  65.  
  66. sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  67. set_var(LEVEL_SET, "_clinumber", buf);
  68.  
  69. oldtitle=(char *)(w->Title);
  70.  
  71. set_var(LEVEL_SET, v_titlebar, shellname);
  72. set_var(LEVEL_SET, v_prompt,
  73.     (IsInteractive(Input())) ? "\23337m%p> \2330m" : "");
  74. set_var(LEVEL_SET, v_hist, "20");
  75. set_var(LEVEL_SET, v_lasterr, "0");
  76. set_var(LEVEL_SET, v_stat, "0");
  77. set_var(LEVEL_SET, v_path, "RAM:,RAM:c/,df0:c/,df1:c/,sys:system/");
  78. set_var(LEVEL_SET, "_insert", "1");
  79. set_var(LEVEL_SET, "f1", "cdir df0:\15");
  80. set_var(LEVEL_SET, "F1", "cdir df1:\15");
  81. set_var(LEVEL_SET, "f3", "cdir RAM:\15");
  82. set_var(LEVEL_SET, "F3", "cdir vd0:\15");
  83. set_var(LEVEL_SET, "f4", "cd df0:\15");
  84. set_var(LEVEL_SET, "F4", "cd df1:\15");
  85. set_var(LEVEL_SET, "f5", "cls; ls\15");
  86. set_var(LEVEL_SET, "F5", "cdir ");
  87. set_var(LEVEL_SET, "f6", "lc\15");
  88. set_var(LEVEL_SET, "f7", "info\15");
  89. set_var(LEVEL_SET, "F7", "assign \15");
  90. set_var(LEVEL_SET, "f8", "window -lf\15");
  91. set_var(LEVEL_SET, "F8", "window -sb\15");
  92. set_var(LEVEL_SET, "f10", "cls\15");
  93. set_var(LEVEL_SET, "F10", "exit\15");
  94. set_var(LEVEL_ALIAS, "cls", "echo -n ^l");
  95. set_var(LEVEL_ALIAS, "lc", "ls -s");
  96. set_var(LEVEL_ALIAS, "kr", "rm -r RAM:* >NIL:");
  97. set_var(LEVEL_ALIAS, "cdir", "%q cd $q; cls; dir");
  98. set_var(LEVEL_ALIAS, "exit", "endcli;quit");
  99. set_var(LEVEL_ALIAS, "lp", "cat >PRT:");
  100. seterr();
  101. if (Myprocess->pr_CurrentDir == NULL)
  102.     do_cd("x :");
  103. else do_pwd(NULL);
  104. Enable_Abort = 0;
  105.  
  106. if (exists(av[1] = "S:.login")) do_source("x S:.login");
  107.  
  108. for (i = 1; i < argc; ++i) {
  109.     if (!strcmp(argv[i],"-c")) {
  110.         Inline[0] = ' ';
  111.         Inline[1] = '\0';
  112.         while (++i < argc)
  113.             { strcat(Inline,argv[i]); strcat(Inline," "); }
  114.         exec_command(Inline);
  115.         main_exit(Lastresult);
  116.         }
  117.     if (!strcmp(argv[i],"-a")) { aux = 1; continue; }
  118.     sprintf (Inline, "source %s",argv[i]);
  119.     av[1] = argv[i];
  120.     do_source (Inline);
  121.     }
  122. for (;;) {
  123.    if (breakcheck())
  124.     while (WaitForChar(Input(), 100L) || stdin->_bp < stdin->_bend)
  125.         gets(Inline);
  126.    clearerr(stdin);  /* prevent acidental quit */
  127. #if RAW_CONSOLE
  128.    if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  129. #else
  130.    printf("%s", disable ? "_ " : trueprompt);
  131.    fflush(stdout);
  132.    if (Quit || !gets(Inline)) main_exit(0);
  133. #endif
  134.    breakreset();
  135.    if (*Inline) exec_command(Inline);
  136.    }
  137. }
  138.  
  139. main_exit(n)
  140. {
  141. register unsigned short i;
  142. char buf[10];
  143.  
  144. Getenv(shellctr,buf,10L);
  145. i=(int)Atol(buf);
  146. sprintf(buf,"%d",i-1);
  147. Setenv(shellctr,buf);
  148. SetWindowTitles(w,oldtitle,-1L);
  149. for (i=1; i<MAXMYFILES; i++) myclose((int)i);
  150. ArpExit(0L,0L);            /* Intuition need not to be closed */
  151. }
  152.  
  153. breakcheck()
  154. {
  155. return (int)(SetSignal(0L,0L) & SIGBREAKF_CTRL_C);
  156. }
  157.  
  158. breakreset()
  159. {
  160. SetSignal(0L, SIGBREAKF_CTRL_C);
  161. }
  162.  
  163. dobreak()
  164. {
  165. if (breakcheck()) { printf("^C\n"); return(1); }
  166. return(0);
  167. }
  168.  
  169. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  170. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  171. /* is zero).  Since we want to check for our own ^C's             */
  172.  
  173. Chk_Abort()
  174. {
  175. return(0);
  176. }
  177.  
  178. _wb_parse()
  179. {
  180. }
  181.  
  182. do_howmany()
  183. {
  184. char buf[10];
  185.  
  186. Getenv(shellctr, buf, 10L);
  187. printf("Shell(s) running: %s\n",buf);
  188. }
  189.  
  190. struct Window *getwindow()
  191. {
  192. struct InfoData *infodata;
  193. struct Window *win;
  194. long args[8];
  195.  
  196. infodata=AllocMem((long)sizeof(struct InfoData),MEMF_CLEAR | MEMF_PUBLIC);
  197. args[0]=(long)infodata >> 2;
  198. SendPacket(ACTION_DISK_INFO,args,Myprocess->pr_ConsoleTask);
  199. win=(struct Window *)infodata->id_VolumeNode;
  200. FreeMem(infodata,(long)sizeof(struct InfoData));
  201. return win;
  202. }
  203.